home *** CD-ROM | disk | FTP | other *** search
/ Aminet 52 / Aminet 52 (2002)(GTI - Schatztruhe)[!][Dec 2002].iso / Aminet / comm / irc / GGSearch.lha / GGSearch.amirx next >
Text File  |  2002-09-16  |  5KB  |  170 lines

  1. /* GGSearch 1.4 ©2002 Michael Trebilcock */
  2. Options Results
  3. Parse Arg Command" "Args
  4.  
  5. Port=Address()
  6.  
  7. UCommand=Upper(Command)
  8. IF UCommand~="SEARCH"&UCommand~="NEXT"|UCommand="SEARCH"&Args="" THEN Call ErrMsg("Usage: /GG <SEARCH/NEXT> [Query]")
  9.  
  10. Bold="02"x;Col="03"x
  11. Res="0F"x;More="0"
  12.  
  13. IF UCommand="NEXT" THEN DO
  14.  Args=GetClip(GGSearch)
  15.  Next=GetClip(GGNext)
  16.  IF Next="" THEN Call ErrMsg("You haven't searched for anything yet!")
  17. End
  18. ELSE Call SetClip(GGSearch,Args)
  19.  
  20. Search=Translate(Args,"+"," ")
  21. Desc=Search;Call FindRepl("& %26");Search=Desc
  22.  
  23. Call cEcho("Connecting to www.google.com, please wait..")
  24. IF ~Open(TCP,"TCP:www.google.com/80","RW") THEN Call ErrMsg("Unable to connect, try again later..")
  25. Call cEcho("Connected, searching for "Args)
  26. IF UCommand="NEXT" THEN Call WriteLn(TCP,"GET "Next" HTTP/1.0")
  27. ELSE Call WriteLn(TCP,"GET /search?q="Search"&btnG=Google+Search HTTP/1.0")
  28. Call WriteLn(TCP,"User-Agent: GGSearch/1.4")
  29. Call WriteLn(TCP,"Host: www.google.com")
  30. Call WriteLn(TCP,"Referer: http://www.google.com/")
  31. Call WriteLn(TCP,"")
  32. IF Word(ReadLn(TCP),2)~="200" THEN Call Layout()
  33.  
  34. DO UNTIL Output="</script>"|EOF(TCP)
  35.  Output=ReadLn(TCP)
  36. End
  37. Call EOFCheck()
  38.  
  39. Call Pointer()
  40.  
  41. IF Pos("web page information",Output)>0 THEN DO
  42.  WebSite="1"
  43.  Call cEcho(Col"0,12Showing web page information for "Bold""Args""Bold)
  44.  i=1;To=1
  45.  Call CheckRes()
  46. End
  47.  
  48. ELSE IF Pos(">Results <",Output)="0" THEN DO
  49.  DO UNTIL EOF(TCP)
  50.   Output=ReadLn(TCP)
  51.   IF Pos("did not match",Output)>0 THEN Call CloseTCP(Bold"End of Google search"Bold" - No matches found")
  52.   IF Pos("no information",Output)>0 THEN DO
  53.    DO UNTIL Pos("Find web pages",Output)>0|EOF(TCP)
  54.     Output=ReadLn(TCP)
  55.    End
  56.    Call EOFCheck()
  57.    Output2=SubStr(Output,Pos("HREF=",Upper(Output))+5)
  58.    Output2=Left(Output2,Pos(">",Output2)-1)
  59.    Call SetClip(GGNext,Output2)
  60.    Call cEcho(Bold"End of Google search"Bold" - No information available for URL "Bold""Args""Bold)
  61.    Call CloseTCP("Type /GG NEXT to find web pages that contain the term "Args)
  62.   End
  63.  End
  64. End
  65. Call EOFCheck()
  66.  
  67. Output2=Output
  68. Output=SubStr(Output,Pos(">Results <",Output))
  69. Output=Translate(Output,,"<>/b")
  70. From=Word(Output,2);To=Word(Output,4)
  71. Sites2=Word(Output,6);Sites=Word(Output,8)
  72. Secs2=Word(Output,10);Secs=Word(Output,12)
  73. IF Sites2="a" THEN Call cEcho(Col"0,12Results "Bold""From" - "To""Bold" of about "Bold""Sites""Bold". Search took "Bold""Secs""Bold" seconds."Res)
  74. ELSE Call cEcho(Col"0,12Results "Bold""From" - "To""Bold" of "Bold""Sites2""Bold". Search took "Bold""Secs2""Bold" seconds."Res)
  75. i=From
  76. Output=Output2
  77.  
  78. CheckRes:
  79. DO UNTIL i=To+1|EOF(TCP)
  80.  IF Pos("<p><a",Output)>0 THEN DO
  81.   Output2=SubStr(Output,Pos("<P><A HREF=",Upper(Output))+11)
  82.   Desc=SubStr(Output2,Pos(">",Output2)+1)
  83.   Desc=SubStr(Desc,1,Pos("</A>",Upper(Desc))-1)
  84.   Call FindRepl("<b> "Bold);Call FindRepl("</b> "Bold)
  85.   Call FindRepl("< <");Call FindRepl("> >")
  86.   Call FindRepl("& &");Call FindRepl("" "d2c(34))
  87.   Output2=SubStr(Output2,1,Pos(">",Output2)-1)
  88.   IF Left(Output2,5)="/url?" THEN DO
  89.    Output2=SubStr(Output2,Pos("q=",Output2)+2)
  90.    Output2=SubStr(Output2,1,Pos("&e=",Output2)-1)
  91.   End
  92.   Call cEcho(Bold""i""Bold". "Output2)
  93.   Call cEcho(Bold"^^"Bold Desc)
  94.   i=i+1
  95.  End
  96.  IF i<To+1 THEN Call Pointer()
  97. End
  98. Call EOFCheck()
  99.  
  100. IF WebSite="1" THEN DO
  101.  DO UNTIL Pos("contain the term",Output)>0|EOF(TCP)
  102.   Output=ReadLn(TCP)
  103.  End
  104.  IF EOF(TCP) THEN Call CloseTCP(Bold"End of Google search"Bold)
  105.  Output2=SubStr(Output,Pos("HREF=",Upper(Output))+5)
  106.  Output2=SubStr(Output2,1,Pos(">",Output2)-1)
  107.  Call SetClip(GGNext,Output2)
  108.  Call CloseTCP(Bold"End of Google search"Bold" - Type /GG NEXT to find web pages that contain the term "Args)
  109. End
  110.  
  111. IF Right(To,1)="0" THEN DO
  112.  Call SetClip(GGNext,"/search?q="Search"&hl=en&lr=&start="To"&sa=N")
  113.  Call CloseTCP(Bold"End of Google search"Bold" - Type /GG NEXT for the next 10 results")
  114. End
  115. ELSE Call CloseTCP(Bold"End of Google search"Bold" - No more matches")
  116. Exit
  117.  
  118. ErrMsg:
  119. Call cEcho(Arg(1))
  120. Exit
  121.  
  122. cEcho:
  123. IF Port="REXX" THEN SAY "«GGSearch» "Arg(1)
  124. ELSE "ECHO P="d2c(27)"b«GGSearch» TEXT="Arg(1)
  125. Return 0
  126.  
  127. CloseTCP:
  128. IF Arg(1)~="" THEN Call cEcho(Arg(1))
  129. Call Close(TCP)
  130. Exit
  131.  
  132. Layout:
  133. Call cEcho(Bold"Error"Bold": Could not search for "Args)
  134. Call cEcho("The page layout has possibly changed")
  135. Call Close(TCP)
  136. Exit
  137.  
  138. FindRepl:
  139. Prstxt=Arg(1)
  140. LFor=Word(Prstxt,1)
  141. Length=Length(LFor)
  142. Rplas=Word(Prstxt,2)
  143. First="0"
  144. DO UNTIL First="0"
  145.  First=Pos(LFor,Desc)
  146.  IF First>0 THEN DO
  147.   First2=SubStr(Desc,1,First-1)
  148.   Next=SubStr(Desc,First+Length)
  149.   Desc=First2""Rplas""Next
  150.  End
  151. End
  152. Return 0
  153.  
  154. EOFCheck:
  155. IF EOF(TCP) THEN Call Layout()
  156. Return 0
  157.  
  158. Pointer:
  159. Output=""
  160. DO UNTIL Pos("<p><a",Output)>0|EOF(TCP)
  161.  Output=Output""ReadCh(TCP,25)
  162. End
  163. Output2=""
  164. DO UNTIL Pos("<br>",Output2)>0|EOF(TCP)
  165.  Output2=Output2""ReadCh(TCP,25)
  166. End
  167. Output=Output""Output2
  168. Call EOFCheck()
  169. Return 0
  170.